<script>
import axios from "axios";

export default {
  name: "HomeMessage",
  data() {
    return {
      list: []
    }
  },
  methods:{
    getList() {
      this.loading = true
      axios.get(`/api/message`)
          .then(res => {
            if(res.data.code === 0) {
              this.loading = false
              this.list = res.data.data
            }else {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    },
    del(id) {
      axios.get(`/api/message/del/${id}`)
          .then(res => {
            if(res.data.code === 0) {
              this.$message.success(res.data.msg)
              this.getList()
            } else  {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    }

  },
  mounted() {
    this.getList()
  }

}
</script>

<template>
  <div>
    <div class="message_box">
      <div class="message_item" v-for="(item,index) in list" :key="index">
        <div style="position: relative">
          <img class="one" :src="require('@/assets/img/第一.png')" alt="" v-show="index === 0">
        </div>
        <el-avatar :src="item.avatar"></el-avatar>

        <div class="m_item_box">
          {{ item.content }}
          <i class="el-icon-close" @click="del(item.id)"></i>
        </div>
      </div>
    </div>

    <h1 v-if="list.length === 0" style="margin-left: 20px;color: #cdcdcd">共收到0条留言</h1>
  </div>
</template>

<style scoped>
.message_box {
  overflow-x: hidden;
  overflow-y: auto;
}

.message_item {
  display: inline-flex;
  font-size: 20px;
  color: #fff;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 10px 12px;
  border-radius: 7px;
  margin: 10px 20px;

  .one {
    position: absolute;
    height: 30px;
    width: 30px;
    top: -15px;
    left: -3px;
  }

  .el-avatar {
    height: 40px;
    width: 40px;
  }

  .m_item_box {
    display: inline-block;
    height: 40px;
    line-height: 40px;
    margin-left: 10px
  }

  .el-icon-close {
    cursor: pointer;
  }

  .el-icon-close:hover {
    color: #f44336;
  }

  animation: scroll 10s linear;
}

@keyframes scroll {
  from {
    opacity: 0;
    transform: translateX(200%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>